var TW = TW || {};

var GRID_SIZE = 64;
var RESET_TIME = 5;

function addWaiting(val,t){
	TW.waitAction += val;
	var x = {
		_reset:false,
		r:function(){
			if(this._reset){
				return;
			}
			
			this._reset = true;
			TW.waitAction -= this.val;
		}
	};
	t = t || RESET_TIME;
	TW.gameScn.scheduleOnce(function(){
		x.r();
	}, t);
	return x;
}

initPlay = function(){	
	TW.evts_card = [];
	TW.evts_tank = [];
	TW.sock.on('gridrow', function(gridrow){
		TW.evts_card.push(gridrow);
	});
	TW.sock.on('wait', function(){
		TW.svrWait = true;
	});
	TW.sock.on('time', function(L){
		L = L/1000;
		showMoveTime(L);
		var area = showMoveArea(TW.self.id, true);
		if(area){
			area.runAction(cc.Sequence.create(cc.DelayTime.create(L), cc.CallFunc.create(function(){
				showMoveArea(TW.self.id, false);
			})));
			
			TW.gameScn.uiLayer.enableSetPos(L);
		}
	});
	TW.sock.removeAllListeners('disconnect');
	TW.sock.on('disconnect', function(){
		cc.Director.getInstance().replaceScene(new LoginScn());
	});
	TW.sock.on('win', function(id){
		TW.gameScn.schedule(function(){
			TW.win = TW.players[id].name;
			TW.svrWait = true;
		},3);
	});
	TW.sock.on('pos1', function(pos){
		createFlag(TW.self.id, pos.x, pos.y);
	});
	
	var bindEvt = function(cmd){
		TW.sock.on(cmd, function(data){
			data.cmd = cmd;
			TW.evts_tank.push(data);
		});
	}
	bindEvt('pos');
	bindEvt('conflict');
	bindEvt('conflictfail');
	bindEvt('ghit');
	bindEvt('gbuff');
	bindEvt('clear');
	bindEvt('shootbox');
	bindEvt('sbhit');
	bindEvt('shootln');
	bindEvt('slhit');
	bindEvt('pick');
}


